<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * TomatoCart Open Source Shopping Cart Solution
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License v3 (2007)
 * as published by the Free Software Foundation.
 *
 * @package		TomatoCart
 * @author		TomatoCart Dev Team
 * @copyright	Copyright (c) 2009 - 2012, TomatoCart. All rights reserved.
 * @license		http://www.gnu.org/licenses/gpl.html
 * @link		http://tomatocart.com
 * @since		Version 2.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Index Controller
 *
 * @package		TomatoCart
 * @subpackage	tomatocart
 * @category	template-module-controller
 * @author		TomatoCart Dev Team
 * @link		http://tomatocart.com/wiki/
 */
class Index extends TOC_Controller
{
    /**
     * Constructor
     *
     * @access public
     * @param string
     */
    public function __construct()
    {
        parent::__construct();

        $this->load->library('desktop_setting');
    }

    /**
     * Default Controller
     * 
     * @access public
     * @return string
     */
    public function index()
    {
        //If it is an ajax request, get the ajax response and add it into the output
        $module = $this->input->get_post('module');
        $action = $this->input->get_post('action');
        if ((!empty($module) && !empty($action)) || (!empty($action)))
        {
            $response = $this->ajax($module, $action);
            
            if (!empty($response)) {
              $this->set_output($response);
            }
        }
        else
        {
            $this->load->library('admin');

            if ($this->admin->is_logged_on())
            {
                $data = array();
                $data['definitions'] = $this->lang->parse_ini_file('ext_desktop.php');

                //add css
                $this->template->add_stylesheet_file(base_url() . 'templates/base/web/css/desktop.css');
                $this->template->add_stylesheet_file(base_url() . 'templates/base/web/css/action.css');
                $this->template->add_stylesheet_file(base_url() . 'templates/base/web/css/icon.css');
                $this->template->add_stylesheet_file(base_url() . 'templates/base/web/css/icons-shortcuts.css');

                //setup view
                $this->template->build('index', $data);
            }
            else
            {
                redirect('login');
            }
        }
    }

    /**
     * Handle ajax request generated by extjs component
     *
     * Run the action in one module and the response must be an array
     *
     * @access  private
     * @return  array
     */

    public function ajax($module, $action)
    {
        $this->lang->ini_load($module . '.php');

        $this->lang->db_load('modules-boxes');
        $this->lang->db_load('modules-content');

        if (!empty($module) && !empty($action))
        {
            require_once 'system/modules/' . $module . '/controllers/' . $module . '.php';
            $this->$module = new $module();

            //$this->load->module($module);

            if (is_callable(array($this->$module, $action)))
            {
                $response = $this->$module->$action();
            }
        }
        else
        {
            $action = $this->input->get_post('action');

            if (method_exists($this, $action))
            {
                $response = $this->$action();
            }
        }

        return $response;
    }

    /**
     *
     */
    public function desktop()
    {
        $data['username'] = $this->desktop_setting->get_username();
        $data['modules'] = $this->desktop_setting->get_modules();
        $data['launchers'] = $this->desktop_setting->get_launchers();
        $data['styles'] = $this->desktop_setting->get_styles();
        $data['output'] = $this->desktop_setting->output_modules();

        //setup view
        $a = $this->template->_find_view('desktop', $data, TRUE);

        echo $a;
    }

    // --------------------------------------------------------------------

    //Following Commands are used to handle the ajax requests generated from the desktop setting

    /**
     *
     */
    private function logoff()
    {
        $this->admin->reset();

        return array('success' => TRUE);
    }

    /**
     *
     */
    private function list_wallpapers()
    {
        $wallpapers = $this->desktop_setting->get_wallpapers();

        return array('wallpapers' => $wallpapers);
    }

    /**
     *
     */
    private function load_modules()
    {
        $desktop_Settings = $this->desktop_setting->get_settings();
         
        $modules = $this->desktop_setting->list_modules($desktop_Settings);

        return $modules;
    }

    /**
     *
     */
    private function save_settings()
    {
        $data = array();
        $response = array();

        $data['autorun'] = $this->input->post('autorun');
        $data['quickstart'] = $this->input->post('quickstart');
        $data['contextmenu'] = $this->input->post('contextmenu');
        $data['shortcut'] = $this->input->post('shortcut');

        $data['theme'] = $this->input->post('theme');
        $data['wallpaper'] = $this->input->post('wallpaper');
        $data['transparency'] = $this->input->post('transparency');
        $data['backgroundcolor'] = $this->input->post('backgroundcolor');
        $data['fontcolor'] = $this->input->post('fontcolor');
        $data['wallpaperposition'] = $this->input->post('wallpaperposition');

        $data['sidebaropen'] = $this->input->post('sidebaropen');
        $data['sidebartransparency'] = $this->input->post('sidebartransparency');
        $data['sidebarbackgroundcolor'] = $this->input->post('sidebarbackgroundcolor');
        $data['sidebargadgets'] = $this->input->post('sidebargadgets');

        if ( $this->desktop_setting->save_desktop($data) )
        {
            $response = array('success' => true, 'feedback' => $this->lang->line('ms_success_action_performed'));
        }
        else
        {
            $response = array('success' => false, 'feedback' => $this->lang->line('ms_error_action_not_performed'));
        }

        return $response;
    }

    /**
     * Load module view
     * 
     * @access public
     * @param module
     * @return string
     */
    public function load_module_view($module = NULL) 
    {
        $this->load->helper('html_output');

        $tmp = explode('-', $module);
        $module = $tmp[0];

        //load module translations
        $this->lang->ini_load($module . '.php');
        
        $output = $this->template->_find_view($module . '/main.php');

        $this->output->set_output($output);
    }
}

/* End of file index.php */
/* Location: ./system/tomatocart/controllers/index.php */